Here are examples of mixed nested loops in Java:
For loop inside While loop
For loop inside While loop
public class Main {
public static void main(String[] args) {
int i = 0;
while(i < 5) { // Outer while loop
for(int j = 0; j < 5; j++) { // Inner for loop
System.out.print("* ");
}
System.out.println();
i++;
}
}
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
While loop inside For loop
While loop inside For loop
public class Main {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) { // Outer for loop
int j = 0;
while(j < 5) { // Inner while loop
System.out.print("* ");
j++;
}
System.out.println();
}
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Do-While loop inside For loop
Do-While loop inside For loop
public class Main {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) { // Outer for loop
int j = 0;
do { // Inner do-while loop
System.out.print("* ");
j++;
} while(j < 5);
System.out.println();
}
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
For loop inside Do-While loop
For loop inside Do-While loop
public class Main {
public static void main(String[] args) {
int i = 0;
do { // Outer do-while loop
for(int j = 0; j < 5; j++) { // Inner for loop
System.out.print("* ");
}
System.out.println();
i++;
} while(i < 5);
}
}
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
In each of these examples, the inner loop executes its iterations for each iteration of the outer loop, printing a row of five asterisks. The outer loop then moves to the next line and the process repeats, resulting in a square pattern of asterisks.